package com.mxpio.erp.inventory.controller;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.InventoryEnums;
import com.mxpio.erp.common.inventory.entity.InventoryPlan;
import com.mxpio.erp.common.inventory.service.InventoryPlanService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;


@Tag(name = "InventoryPlanController}", description = "盘点计划")
@RestController
@RequestMapping("/erp/inventory/invplan/")
public class InventoryPlanController {

    @Autowired
    private InventoryPlanService service;


    @GetMapping("list")
    @Operation(summary = "盘点计划列表", description = "获取盘点计划列表（无分页）", method = "GET")
    public Result<List<InventoryPlan>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InventoryPlan> list = service.list(InventoryPlan.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "盘点计划列表", description = "获取盘点计划列表（分页）", method = "GET")
    public Result<Page<InventoryPlan>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InventoryPlan> pageResult = service.listPage(InventoryPlan.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{code}")
    @Operation(summary = "根据ID获取盘点计划列表", description = "根据ID获取盘点计划列表", method = "GET")
    public Result<InventoryPlan> getById(@PathVariable(name = "code", required = true) String code) {
        InventoryPlan plan = service.getById(InventoryPlan.class, code);
        return Result.OK(plan);
    }

    @RequestMapping("add")
    @Operation(summary = "保存盘点计划", description = "保存盘点计划")
    public Result<InventoryPlan> add(@RequestBody InventoryPlan plan) {
        plan.setLastPlanStart(new Date());//设置默认值
        plan.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));//默认未开启
        plan.setExecStatus(InventoryEnums.PlanExecStatus.UNEXEC.getCode());//默认未执行
        service.save(plan);
        return Result.OK(plan);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑盘点计划", description = "编辑盘点计划", method = "PUT")
    public Result<InventoryPlan> edit(@RequestBody InventoryPlan plan) {
        service.update(plan);
        return Result.OK(plan);
    }
    @GetMapping("enable/{code}")
    @Operation(summary = "根据ID启用盘点计划", description = "根据ID启用盘点计划", method = "GET")
    public Result<InventoryPlan> enable(@PathVariable(name = "code", required = true) String code) {
        InventoryPlan byId = service.getById(InventoryPlan.class, code);
        if (StringUtils.equals(byId.getIsEnable().toString(),CommonEnums.YesNo.YES.getCode())){
            return Result.error("计划已经是已生效状态！");
        }
        if (StringUtils.equals(byId.getExecStatus(),InventoryEnums.PlanExecStatus.FINISH.getCode())){
            return Result.error("已完成计划不允许重复启用！");
        }
        if (StringUtils.equals(byId.getExecStatus(),InventoryEnums.PlanExecStatus.SCRAP.getCode())){
            return Result.error("已作废计划不允许重复启用！");
        }
        byId.setIsEnable(Integer.parseInt(CommonEnums.YesNo.YES.getCode()));
        service.update(byId);
        return Result.OK(byId);
    }
    @GetMapping("disable/{code}")
    @Operation(summary = "根据code关闭/禁用盘点计划", description = "根据code关闭/禁用盘点计划", method = "GET")
    public Result<InventoryPlan> disable(@PathVariable(name = "code", required = true) String code) {
        InventoryPlan byId = service.getById(InventoryPlan.class, code);
        if (StringUtils.equals(byId.getIsEnable().toString(),CommonEnums.YesNo.NO.getCode())){
            return Result.error("计划已经是禁用状态！");
        }
        if (!(StringUtils.equals(byId.getExecStatus(),InventoryEnums.PlanExecStatus.UNEXEC.getCode())
                ||StringUtils.equals(byId.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode()))){
            return Result.error("仅未执行或者执行中状态的计划可以禁用！");
        }
        byId.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));
        service.update(byId);
        return Result.OK(byId);
    }
    @DeleteMapping("remove/{codes}")
    @Operation(summary = "根据code删除盘点计划", description = "删除盘点计划,多个code用英文逗号分隔", method = "DELETE")
    public Result<InventoryPlan> remove(@PathVariable(name = "codes", required = true) String codes) {
        String[] code = codes.split(",");
        for(String key : code){
            service.delete(InventoryPlan.class, key);
        }
        return Result.OK();
    }
}
